package models;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sprites.ActorSprite;
import sprites.Sprite;

public class MainWindowState implements MainWindowStateInterface {
	

	private	ArrayList<Sprite> 		sprites	= new ArrayList<Sprite>();
	private ArrayList<ActorSprite>	actors	= new ArrayList<ActorSprite>();
	
	private	Sprite					selectedSprite = null;
	private ActorSprite				selectedActor = null;
	private ActorSprite				currentTurn = null;

	private static final boolean myEquals(Object A, Object B) {
		if (A==null) { 
			return B==null;
		} else if (B==null) { 
			return false;
		} else {
			return A.equals(B);
		}
	}
	
	@Override
	public boolean containsSprite(Sprite S) {
		return sprites.contains(S);
	}

	@Override
	public boolean containsActor(ActorSprite A) {
		return actors.contains(A);
	}

	@Override
	public List<Sprite> getAllSprites() {
		List<Sprite> L = new ArrayList<Sprite>();
		L.addAll(sprites);
		return L;
	}

	@Override
	public List<ActorSprite> getAllActors() {
		List<ActorSprite> L = new ArrayList<ActorSprite>();
		L.addAll(actors);
		return L;
	}

	@Override
	public void setSelectedSprite(Sprite S) {
		Sprite	old = getSelectedSprite();
		if (!myEquals(old, S)) {
			this.selectedSprite = S;
		}
	}

	@Override
	public void setSelectedActor(ActorSprite A) {
		ActorSprite	old = getSelectedActor();
		if (!myEquals(old, A)) {
			this.selectedActor = A;
		}
	}

	@Override
	public void setCurrentTurn(ActorSprite A) {
		ActorSprite old = getCurrentTurn();
		if (!myEquals(old,A)) {
			this.currentTurn = A;
		}
	}

	@Override
	public ActorSprite getCurrentTurn() {
		return this.currentTurn;
	}

	@Override
	public ActorSprite getSelectedActor() {
		return this.selectedActor;
	}

	@Override
	public Sprite getSelectedSprite() {
		return this.selectedSprite;
	}

	@Override
	public void addSprite(Sprite S) {
		sprites.add(S);
		if (S.getClass().equals(ActorSprite.class)) {
			actors.add((ActorSprite) S);
		}
	}

	@Override
	public void removeSprite(Sprite S) {
		if (containsSprite(S)) {
			sprites.remove(S);
			if (S.getClass().equals(ActorSprite.class)) {
				actors.remove((ActorSprite) S);
			}
		}
	}

	@Override
	public void insertSprite(Sprite S, int turn) {
		
	}

	@Override
	public void sortActors() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int indexOf(ActorSprite A) {
		return actors.indexOf(A);
	}

	@Override
	public ArrayList<Sprite> getSpritesUnderPoint(Point P) {
		Iterator<Sprite>	itr = sprites.iterator();
		Sprite				S;
		ArrayList<Sprite>	L = new ArrayList<Sprite>();
		
		while(itr.hasNext()) {
			S = itr.next();
			if (S.containsPoint(P)) {
				L.add(S);
			}
		}
		
		return L;
	}

	@Override
	public ArrayList<ActorSprite> getActorsUnderPoint(Point P) {
		Iterator<ActorSprite>	itr = actors.iterator();
		ActorSprite				S;
		ArrayList<ActorSprite>	L = new ArrayList<ActorSprite>();
		
		while(itr.hasNext()) {
			S = itr.next();
			if (S.containsPoint(P)) {
				L.add(S);
			}
		}
		
		return L;
	}

	@Override
	public void fromXml(String xml) {
		// TODO: Write this in the future for loading from file
	}

	@Override
	public String toXml() {
		// TODO Auto-generated method stub
		return null;
	}

}
